import matplotlib.pyplot as plt
import numpy as np
from matplotlib.legend_handler import HandlerLine2D, HandlerTuple

# 准备数据

x = np.array([1,5,10,20,50,100,150])
x2 = np.array([1,5,13,25,50,100,150])

# x = np.logspace(-1, 0,n)

y11 = np.array([14.75,14.53,14.12,14.50,13.44,13.50192,13.102955])
y12 = np.array([15.22,15.30829667,15.39122167,15.66,15.46,15.06,14.76])#ept



y21 = np.array([80.17, 80.15,80.38,79.89,80.64,80.248555,80.15985649])
y22 = np.array([80.06033667,80.34788333,80.82603333,81.03,80.18,80.47,81.08]) #ept




y31 = np.array([19.62, 20.32,19.81,20.41,19.69,18.81779333,18.86885614])
y32 = np.array([19.06659667, 19.25882167,19.48611, 19.02,20.16,21.41,21.84])#ept


# 创建图形和坐标轴对象
fig, axs = plt.subplots(1, 3, figsize=(15, 4.5))

legend_handles = []
legend_labels = ['ViT', 'SViT']


# 绘制第一个图
#=========vpt===============
axs[0].plot(x, y11, label='VPT', color='red', linestyle='-.')
axs[0].scatter(x, y11, color='red', marker='s', s=25)

#=========full===============
axs[0].plot(x2, y12, label='Full', color='steelblue', linestyle='-.')
axs[0].scatter(x2, y12, color='steelblue', marker='s', s=25)

axs[0].set_xticks(x)
# axs[0].set_xticklabels(['$10^{-1}$', '','','', '$10^{0}$'])
axs[0].grid(True, which='both', color='lightgray', alpha=0.8)

# 绘制第二个图
axs[1].plot(x, y21, label='VPT', color='red', linestyle='-.')
axs[1].scatter(x, y21, color='red', marker='s', s=25)

#=========full===============
axs[1].plot(x2, y22, label='Full', color='steelblue', linestyle='-.')
axs[1].scatter(x2, y22, color='steelblue', marker='s', s=25)

axs[1].set_xticks(x)
# axs[1].set_xticklabels(['$10^{-1}$', '','','', '$10^{0}$'])
axs[1].grid(True, which='both', color='lightgray', alpha=0.8)

# 绘制第三个图
axs[2].plot(x, y31, label='VPT', color='red', linestyle='-.')
axs[2].scatter(x, y31, color='red', marker='s', s=25)

#=========full===============
axs[2].plot(x2, y32, label='Full', color='steelblue', linestyle='-.')
axs[2].scatter(x2, y32, color='steelblue', marker='s', s=25)

axs[2].set_xticks(x)
# axs[2].set_xticklabels(['$10^{-1}$', '','','', '$10^{0}$'])
axs[2].grid(True, which='both', color='lightgray', alpha=0.8)

# 共用一个图例

for i in range(3):
    legend_handles.append((axs[i].get_lines()[0], axs[i].collections[0]))
    legend_handles.append((axs[i].get_lines()[1], axs[i].collections[1]))
    # legend_handles.append((axs[i].get_lines()[2], axs[i].collections[2]))
    # legend_handles.append((axs[i].get_lines()[3], axs[i].collections[3]))
# fig.legend(handles=legend_handles, labels=legend_labels, loc=(0.79, 0.9), ncol=n)
fig.legend(handles=legend_handles, labels=legend_labels, fontsize=18, loc='upper right', ncol=4)


# 在scatter图中添加注释
annotations = ['1', '5', '10', '20','50','100','150']
annotations2 = ['1', '4', '12', '25','50','100','150']
#1,2,4,12,25,50,100,150
for j, txt in enumerate(annotations):
    axs[0].annotate(txt, (x[j], y11[j]), xytext=(0, 0), textcoords='offset points')
    axs[1].annotate(txt, (x[j], y21[j]), xytext=(0, 0), textcoords='offset points')
    axs[2].annotate(txt, (x[j], y31[j]), xytext=(0, 0), textcoords='offset points')



for j, txt in enumerate(annotations2):
    axs[0].annotate(txt, (x2[j], y12[j]), xytext=(0, 0), textcoords='offset points')
    axs[1].annotate(txt, (x2[j], y22[j]), xytext=(0, 0), textcoords='offset points')
    axs[2].annotate(txt, (x2[j], y32[j]), xytext=(0, 0), textcoords='offset points')


# 在图的最左侧显示y轴标签
axs[0].set_ylabel('Test accuracy(%)', fontsize=18, fontweight='normal')
axs[0].set_title('Chest(3)', fontsize=18, fontweight='normal')
axs[1].set_title('Colon(3)', fontsize=18, fontweight='normal')
axs[2].set_title('Endo(3)', fontsize=18, fontweight='normal')


# 调整子图的间距
fig.tight_layout()
plt.subplots_adjust(top=0.8)
# plt.subplots_adjust(bottom=0.2)

# plt.subplots_adjust(left=0.06)
# plt.subplots_adjust(right=0.95)

# fig.text(0.5, 0.05, 'Tunable parameters w.r.t. backbone (%)', ha='center', va='center')

# 保存图形

plt.savefig('Ablation_results/2_figures_prompt_length.png')